from PyQt6.QtCore import QThread, pyqtSignal, QCoreApplication
import Global

from MyMessageBox import MyMessageBox


class HeartThread(QThread):
    update_Heart_signal = pyqtSignal()  # 定义一个信号，用于更新表格

    def __init__(self, parent=None):
        super().__init__(parent)
        self.running = True

    def run(self):
        card_value = Global.read_ini(Global.ConfigFileName, 'System', 'card', '')
        erroCount=0
        while self.running:
            self.update_Heart_signal.emit()  # 发送信号，触发表格更新
            try:
                isSuccess, erro = Global.G_YZ.Heart()
                if not isSuccess:
                    if erroCount > 5:
                        # Global.KillProcess(Global.G_Game[0].data["processID"])
                        Global.LOGE(f"验证错误:{erro}")
                        MyMessageBox.show("验证错误", erro)
                        QCoreApplication.exit()
                        break
                    else:
                        erroCount +=1
                        self.sleep(int(Global.G_YZ.HEART_TIME / 10))#如果验证错误,后重试
                        continue
                erroCount =0
                isSuccess, resp = Global.G_YZ.GetCardInfo(card_value)
                #isSuccess, resp = Global.G_YZ.cloudFunc(Global.G_YZ.GetCardInfo_ID, "GetCardInfo")
                if isSuccess:
                    Global.mainWindow.expire_value.setText(str(resp.get("endTime", "")))
                self.sleep(Global.G_YZ.HEART_TIME)  # 根据心跳间隔刷新
            except Exception as e:
                Global.LOGE(f"心跳异常:{e}")

    def stop(self):
        self.running = False